//2009-12-30
#ifndef SIMULATOR_H
#define SIMULATOR_H

#include <QGraphicsView>
#include <QGraphicsItem>
#include <QGraphicsLineItem>
#include <QString>
#include <QLabel>
#include <QPushButton>
#include <QComboBox>
#include <QLineEdit>
#include <QTime>
#include <fstream>
#include <gsl/gsl_randist.h>
#include "networkplanning.h"
#include "user.h"
#include "basestation.h"
#include "hotspot.h"

enum TrafficPattern
{
	Day,
	Night,
	More,
};

class MapBlock : public QGraphicsItem
{
public:
	MapBlock( int X, int Y );
	void   SetBSExist( bool flag );
	bool   GetBSExist();
	int    GetUserNumber() const;
	void   SetUserNumber(int user);
	int    GetX() const;
	int    GetY() const;
	bool   GetUserCovered() const;
	void   SetUserCovered( bool flag );
	double GetTotalDayTraffic() const;
	double GetTotalNightTraffic() const;

	void   SetServeUnit(RFUnit* unit);
	RFUnit* GetServeUnit() const;
	void   SetServeChannel(int chan);
	int    GetServeChannel() const;
	void   CalculateTraffic();

	// for GUI
	QRectF boundingRect() const;
	void   paint( QPainter* painter , const QStyleOptionGraphicsItem* option , QWidget* widget );

private:
	int    x, y;						// position
	int    userNumber;					// number of users
	RFUnit* serveUnit;		    		// number of serving BS
	int    serveChannel;                // number of serving channel

	//double *powerBS;					// Received BS power(dBm)

	double totalDayTraffic;
	double totalNightTraffic;
	bool   BSExist;
	bool   userCovered;

friend class CityMap;
friend class UserProperty;
friend class BaseStation;
friend class Simulator;

};

class CityMap : public QGraphicsView
{
	Q_OBJECT
signals:
	void   Show(int x, int y);
	void   valueChanged(int new_scale);
	void   SetHotspotCenter(int x, int y);

public:
	CityMap( QWidget *parent = 0 );
	~CityMap();
	void   GenerateCityBlock();
	void   GenerateTraffic();
	void   RepaintBlock( int i , int j );
	void   RepaintAll();
	int    numHotspot;
	static enum DisplayLayer{
	   TRAFFIC,
	   COVERAGE,
	} layer;

public slots:
	void   Rescale (int new_scale);

private:
	QLabel *labelMousePos;
	QGraphicsScene *Scene;
	QGraphicsLineItem *arrow;
	int    m_scale;
	QPoint s1,s2;
	QTime  time;

protected:
	void   wheelEvent( QWheelEvent *event );
	void   mousePressEvent( QMouseEvent *event );
	void   mouseReleaseEvent( QMouseEvent *event );
	void   mouseMoveEvent( QMouseEvent *event );
};

class Simulator : public QWidget
{
	Q_OBJECT
public:
	Simulator();
	~Simulator();
	void   CalculateBSState();
	static bool IsDay();
	double Simulation(int step);
	static void SetTrafficPattern( enum TrafficPattern );

public slots:
	void   InitBS();
	void   StartBSSimulation();
	void   ResetBSSimulation();
	void   InputHotSpots(int num);
	void   DisplayPosition(int x, int y);
	void   InputBSSites();
	void   SetHotspot();
	void   DisplayLayer(bool checked);
	void   SetTrafficPattern(int ptn);

private:
	QLabel    *SetTrafficLabel;
	QComboBox *SetTraffic;
	
	//DEBUG!!!!
	QSpinBox  *SimuStep;

	QLabel    *SetHotSpots;
	QSpinBox  *HotSpots;
	Hotspot_Config  *hotspot_config[MAXHOTSPOT];

	
	QLabel    *ShowCoverRate;
	QLineEdit *CoverRate;

	QLabel    *ShowTotalUserNumber;
	QLineEdit *TotalUserNumber;

	QLabel    *ShowServedUserNumber;
	QLineEdit *ServedUserNumber;

	QPushButton *SetHotNumber;
	QPushButton *StartSimulation;
	QPushButton *Reset;
	QPushButton *Layer;

	QLabel *labelMousePos;

	CityMap   *citymap;

	static TrafficPattern pattern;
};

#endif /* SIMULATOR_H */
